//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.tuliu.appupdate.updatelib;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;
import android.util.Log;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author liaoyong
 * @desc 功能描述
 * @date 2016/4/11 13:57
 */
public class UpdateManager {
    static final int ERROR_OK = -1;
    static final int ERROR_FAIL = 2;
    static final int RESULT_START = 17;
    static final int RESULT_PROGRESSING = 18;
    static final int RESULT_COMPLETE = 19;
    public static final int RESULT_ClickBACK = 20;
    public static final int RESULT_ClickCancel = 21;

    public static final String KEY_DOWNLOAD_FILE_PATH = "download_file_path";
    static final String KEY_PROGRESS = "progress";
    static final String KEY_MAX = "max";
    public static final String KEY_ERROR_CODE = "error_code";
    public static final String KEY_ERROR_MSG = "error_msg";
    public static final String KEY_UPDATE_INFO = "UpdateInfo";
    public static final String KEY_ERROR_EXT_CODE = "error_ext_code";
    public static final String KEY_EXCEPTION_CLASS_NAME = "ExceptionClassName";
    public static final String KEY_EXCEPTION_STACK_TRACE = "ExceptionStackTrace";
    private static final String TAG = "UpdateManager";
    private static AtomicBoolean sDebug = new AtomicBoolean(false);
    private static AtomicInteger sTestMode = new AtomicInteger(0);

    public UpdateManager() {
    }

    public static boolean isDebug() {
        return sDebug.get();
    }

    public static void setDebug(boolean debug) {
        sDebug.set(debug);
    }

    public static void setTestMode(int testMode) {
        sTestMode.set(testMode);
    }

    public static boolean isTestMode() {
        return sTestMode.get() != 0;
    }

    static int getTestMode() {
        return sTestMode.get();
    }


    public static void checkUpdate(Context context, boolean notification, boolean autoShowDialogOrNotification, String checkUrl, UpdateManager.CheckUpdateListener listener) {
        try {
            UpdateManager.CheckUpdateResultReceiver e = null;
            if(listener != null) {
                e = new UpdateManager.CheckUpdateResultReceiver(listener);
            }

            CheckUpdateService.checkUpdate(context, context.getPackageName(), notification, checkUrl, e, autoShowDialogOrNotification);
        } catch (Exception var6) {
            if(isDebug()) {
                Log.e(TAG, "checkUpdate", var6);
            }
        }

    }


    public static boolean doUpdate(Context context, AppInfo updateInfo) {
        return doUpdate(context, updateInfo, true, true, (UpdateManager.DownloadListener)null);
    }

    public static boolean doUpdate(Context context, AppInfo updateInfo, boolean auotoInsall, boolean showNotification, UpdateManager.DownloadListener listener) {
        try {
            if(updateInfo.isNewVersion) {
                UpdateManager.DownloadResultReceiver e = listener != null?new UpdateManager.DownloadResultReceiver(listener):null;
                DownloadInfo info = new DownloadInfo(context, updateInfo);
                info.autoInstall = auotoInsall;
                info.showNotification = showNotification;
                CheckUpdateService.startDownload(context.getApplicationContext(), info, e);
                return true;
            } else {
                return false;
            }
        } catch (Exception var7) {
            if(isDebug()) {
                Log.w(TAG, "doDirectDownload fail ", var7);
            }

            return false;
        }
    }


    private static class DownloadResultReceiver extends ResultReceiver implements UpdateManager.DownloadListener {
        private UpdateManager.DownloadListener mListener;

        public DownloadResultReceiver(UpdateManager.DownloadListener listener) {
            super((Handler)null);
            this.mListener = listener;
        }

        public void onDownloadStart(AppInfo updateInfo) {
            if(this.mListener != null) {
                this.mListener.onDownloadStart(updateInfo);
            }

        }

        public void onDownloadProgress(AppInfo updateInfo, long progress, long max) {
            if(this.mListener != null) {
                this.mListener.onDownloadProgress(updateInfo, progress, max);
            }

        }

        public void onDownloadCompleted(AppInfo updateInfo, boolean status, Bundle data) {
            if(this.mListener != null) {
                this.mListener.onDownloadCompleted(updateInfo, status, data);
            }

        }

        protected void onReceiveResult(int resultCode, Bundle resultData) {
            AppInfo updateInfo = (AppInfo)resultData.getParcelable("UpdateInfo");
            long progress = resultData.getLong(KEY_PROGRESS);
            long max = resultData.getLong(KEY_MAX);
            if(resultCode == RESULT_START) {
                this.onDownloadStart(updateInfo);
            } else if(resultCode == RESULT_PROGRESSING) {
                this.onDownloadProgress(updateInfo, progress, max);
            } else if(resultCode == RESULT_COMPLETE) {
                int errorCode = resultData.getInt(KEY_ERROR_CODE);
                this.onDownloadCompleted(updateInfo, errorCode == ERROR_OK, resultData);
            } else if(UpdateManager.isDebug()) {
                Log.e(TAG, "Unkown request code:" + resultCode);
            }

        }
    }

    public interface DownloadListener {
        void onDownloadStart(AppInfo var1);

        void onDownloadProgress(AppInfo var1, long var2, long var4);

        void onDownloadCompleted(AppInfo var1, boolean var2, Bundle var3);
    }

    private static class CheckUpdateResultReceiver extends ResultReceiver {
        private UpdateManager.CheckUpdateListener mListener;

        public CheckUpdateResultReceiver(UpdateManager.CheckUpdateListener listener) {
            super((Handler)null);
            this.mListener = listener;
        }

        protected void onReceiveResult(int resultCode, Bundle resultData) {
            super.onReceiveResult(resultCode, resultData);

            try {
                if(this.mListener != null) {
                    if(resultData == null) {
                        resultData = new Bundle();
                        resultData.putInt(KEY_ERROR_CODE, ERROR_FAIL);
                    }

                    int errorCode = resultData.getInt(KEY_ERROR_CODE);
                    int extCode = resultData.getInt(KEY_ERROR_EXT_CODE);
                    this.mListener.onResult(errorCode == ERROR_OK, resultData, extCode);
                }
            } catch (Exception var4) {
                if(UpdateManager.isDebug()) {
                    Log.e(TAG, "onReceiveResult", var4);
                }

                this.mListener.onResult(false, new Bundle(), 0);
            }

        }
    }

    public interface CheckUpdateListener {
        //参数1：信息获取成功; 参数2：信息model; 参数：ext1,扩展参数
        void onResult(boolean isSuccess, Bundle appInfo, int ext);
    }
}
